package com.ysd.lis.controller.interfacePlatform;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysd.common.Result;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.SysInterface;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.interfacePlatform.SysInterfaceEventLog;
import com.ysd.lis.entity.interfacePlatform.SysInterfaceFunction;
import com.ysd.lis.entity.lab.LabReqMain;
import com.ysd.lis.mapper.interfacePlatform.SysInterfaceEventLogMapper;
import com.ysd.lis.mapper.sys.SysInterfaceMapper;
import com.ysd.lis.request.InterfaceFunctionDto;
import com.ysd.lis.request.InterfaceLogDto;
import com.ysd.lis.service.interfacePlatform.SysInterfaceFunctionService;
import com.ysd.util.RedisUserManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 平台字典表
 * 前端控制器
 * </p>
 *
 * @author pengchong
 * @since 2024-07-15
 */
@Api(tags = "平台系统类型表")
@RestController
@RequestMapping("/api/sys/SysInterfaceFunction")
public class SysInterfaceFunctionController extends BaseController {
    @Autowired
    SysInterfaceFunctionService sysInterfaceFunctionService;
    @Autowired
    SysInterfaceEventLogMapper sysInterfaceEventLogMapper;
    @Autowired
    SysInterfaceMapper sysInterfaceMapper;

    @PostMapping("/addSysInterfaceFunction")
    @LogDetail(requestParam = "addSysInterfaceFunction", operationType = OperationType.INSERT, module = "字典设计", content = "通过字典分类列表树，增加【name】树节点", businessName = "name")
    public Result addSysInterfaceFunction(@RequestBody List<SysInterfaceFunction> sysInterfaceFunctions) {
        return sysInterfaceFunctionService.addSysInterfaceFunction(sysInterfaceFunctions);
    }

    @PostMapping("/editSysInterfaceFunction")
    @LogDetail(requestParam = "editSysInterfaceFunction", operationType = OperationType.UPDATE, module = "字典设计", content = "通过字典分类列表树，编辑【name】树节点", businessName = "name")
    public Result editSysInterfaceFunction(@RequestBody SysInterfaceFunction sysInterfaceFunction) {
        return sysInterfaceFunctionService.editSysInterfaceFunction(sysInterfaceFunction);
    }

    @PostMapping("/deletSysInterfaceFunction")
    @LogDetail(requestParam = "deletSysInterfaceFunction", operationType = OperationType.DELETE, module = "字典设计", content = "通过字典分类列表树模块，删除【name】数据", businessName = "name")
    public Result deletSysInterfaceFunction(@RequestBody SysInterfaceFunction sysInterfaceFunction) {
        return sysInterfaceFunctionService.deletSysInterfaceFunction(sysInterfaceFunction);
    }


    @ApiOperation("查询字典分类树形结构")
    @GetMapping("/findSysInterfaceFunctionTree")
    public Result findSysInterfaceFunctionTree(InterfaceFunctionDto pageParam) {
        return sysInterfaceFunctionService.findSysInterfaceFunction(pageParam);
    }

    @ApiOperation("查询字典系统模块")
    @GetMapping("/findSysInterfaceModule")
    public Result findSysInterfaceModule() {
        return sysInterfaceFunctionService.findSysInterfaceModule();
    }

    @ApiOperation("查询日志表")
    @GetMapping("/findSysInterfaceModuleLog")
    public Result findSysInterfaceModuleLog(InterfaceLogDto interfaceLogDto) {
        Page<SysInterfaceEventLog> page = new Page<>();
        page.setSize(interfaceLogDto.getPageSize());
        page.setCurrent(interfaceLogDto.getPageIndex());
        LambdaQueryWrapper<SysInterfaceEventLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysInterfaceEventLog::getDelFlag, 0)
                .like(StrUtil.isNotEmpty(interfaceLogDto.getOperatorId()), SysInterfaceEventLog::getOperatorId, interfaceLogDto.getOperatorId())
                .like(StrUtil.isNotEmpty(interfaceLogDto.getModule()), SysInterfaceEventLog::getModule, interfaceLogDto.getModule())
                .ge(BeanUtil.isNotEmpty(interfaceLogDto.getStartTime()), SysInterfaceEventLog::getOperateTime, interfaceLogDto.getStartTime())
                .le(BeanUtil.isNotEmpty(interfaceLogDto.getEndTime()), SysInterfaceEventLog::getOperateTime, interfaceLogDto.getEndTime())
                .eq(StrUtil.isNotEmpty(interfaceLogDto.getFlag()), SysInterfaceEventLog::getFlag, interfaceLogDto.getFlag())
                .eq(StrUtil.isNotEmpty(interfaceLogDto.getBusinessName()), SysInterfaceEventLog::getBusinessName, interfaceLogDto.getBusinessName())
                .eq(StrUtil.isNotEmpty(interfaceLogDto.getKeywords()), SysInterfaceEventLog::getReqParam, interfaceLogDto.getKeywords())
                .orderByDesc(SysInterfaceEventLog::getCreateTime);
        return Result.succ(sysInterfaceEventLogMapper.selectPage(page, lambdaQueryWrapper));
    }

    @ApiOperation("查询日志类型")
    @GetMapping("/findSysInterfaceModuleLogType")
    public Result findSysInterfaceModuleLogType() {
        List<Map> returnList = new ArrayList<>();
        Map allMap = new HashMap();
        allMap.put("name", "全部");
        allMap.put("value", "");
        returnList.add(allMap);
        LambdaQueryWrapper<SysInterfaceEventLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysInterfaceEventLog::getDelFlag, 0);
        List<SysInterfaceEventLog> sysInterfaceEventLogs = sysInterfaceEventLogMapper.selectList(lambdaQueryWrapper);
        List<String> collect = sysInterfaceEventLogs.stream().map(SysInterfaceEventLog::getBusinessName).distinct().collect(Collectors.toList());
        for (String s : collect) {
            Map<String, String> map = new java.util.HashMap<>();
            map.put("name", s);
            map.put("value", s);
            returnList.add(map);
        }
        return Result.succ(returnList);
    }

    @ApiOperation("重新调用接口")
    @PostMapping("/reCallInterface")
    public Result reCallInterface(@RequestBody SysInterfaceEventLog sysInterfaceEventLog) {
        String requestUrl = sysInterfaceEventLog.getRequestUrl();
        String reqParam = sysInterfaceEventLog.getReqParam();
        String requestMethod = sysInterfaceEventLog.getRequestMethod();
        SysUser user = RedisUserManager.getUser();
        LambdaQueryWrapper<SysInterface> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysInterface::getDelFlag, 0);
        wrapper.eq(SysInterface::getIsEnable, 1);
        wrapper.eq(SysInterface::getOrgId, user.getOrgId());
        List<SysInterface> sysInterfaces = sysInterfaceMapper.selectList(wrapper);
        List<String> collect = sysInterfaces.stream().filter(o -> Objects.nonNull(o) && StringUtils.isNotBlank(o.getInterfaceUrl())).map(SysInterface::getInterfaceUrl).distinct().collect(Collectors.toList());
        //标本签收
        if (CollectionUtils.isNotEmpty(collect)) {
            for (String webUrl : collect) {
                String url = webUrl + requestUrl;
                HttpUtil.createPost(url).setConnectionTimeout(3000).body(reqParam).contentType("application/json").execute().body();
            }
        }
        return Result.succ(1, "调用成功", "");
    }
}
